﻿Public Class CommonMail
    Function ToGiaiMa112Email(ByVal msText As String) As String
        Try
            msText = Replace(msText, Chr(13), "")
            msText = Replace(msText, Chr(10), "")
            Dim iPos As Integer
            Try
                iPos = msText.LastIndexOf("[TTTHmail]")
                If iPos <> -1 Then
                    msText = msText.Substring(0, iPos)
                End If
            Catch ex As Exception

            End Try


            Dim nC
            Dim nD
            Dim nE
            Dim lA
            Dim lB
            Dim lI
            Dim lJ
            Dim lK
            Dim sB
            lA = Len(msText)
            lB = lA - 1 - (lA - 1) \ 4
            sB = Space(lB)
            For lI = 1 To lB
                lJ = lJ + 1
                nC = Asc(Mid(msText, lJ, 1)) - 59
                Select Case lI Mod 3
                    Case 1
                        lK = lK + 4
                        If lK > lA Then lK = lA
                        nE = Asc(Mid(msText, lK, 1)) - 59
                        nD = ((nE \ 16) And 3) * 64
                    Case 2
                        nD = ((nE \ 4) And 3) * 64
                    Case 0
                        nD = (nE And 3) * 64
                        lJ = lJ + 1
                End Select
                sB = Mid(sB, 1, lI - 1) & Chr(nC Or nD) & Mid(sB, lI + 1, Len(sB) - lI)
            Next
            ToGiaiMa112Email = sB
        Catch ex As Exception

        End Try

    End Function
    Function ToMahoa112Email(ByVal msText As String) As String
        Dim nC
        Dim lI
        Dim lJ
        Dim nK
        Dim lA
        Dim sB
        lA = Len(msText)
        sB = Space(lA + (lA + 2) \ 3)
        For lI = 1 To lA
            nC = Asc(Mid(msText, lI, 1))
            lJ = lJ + 1
            sB = Mid(sB, 1, lJ - 1) & Chr((nC And 63) + 59) & Mid(sB, lJ + 1, Len(sB) - lJ)
            Select Case lI Mod 3
                Case 1
                    nK = nK Or ((nC \ 64) * 16)
                Case 2
                    nK = nK Or ((nC \ 64) * 4)
                Case 0
                    nK = nK Or (nC \ 64)
                    lJ = lJ + 1
                    sB = Mid(sB, 1, lJ - 1) & Chr(nK + 59) & Mid(sB, lJ + 1, Len(sB) - lJ)
                    nK = 0
            End Select
        Next
        If lA Mod 3 Then
            lJ = lJ + 1
            sB = Mid(sB, 1, lJ - 1) & Chr(nK + 59) & Mid(sB, lJ + 1, Len(sB) - lJ)
        End If

        Dim s As String
        s = ToGiaiMa112Email(sb)

        ToMahoa112Email = sB
    End Function
    Public Function GetContentNodeFromEml(ByVal ContentEml As String, ByVal StartFeld1 As String, ByVal StartFeld2 As String) As String
        '----------------------------------------------------------------------------------
        '   Purpose:    Hàm đọc nội dung node xml của email
        '   Parameter:   
        '                           
        '
        '   Author:     Nguyễn Đình Thản
        '   Email:      dinh.than@yahoo.com
        '   Phone:      0903 664 801

        '   Return;     trả về nội dung đọc được
        '   Date;       13/07/2006
        '-----------------------------------------------------------------------------------


        Dim sRet As String = ""
        Dim iLen, iLen1, iLen2, iLenNoiDung As Integer
        Dim iVT1, iVT2 As Integer

        Try
            iLen = Len(ContentEml)
            iLen1 = Len(StartFeld1)
            iLen2 = Len(StartFeld2)
            iVT1 = InStr(1, ContentEml.ToLower, StartFeld1.ToLower) + iLen1
            iVT2 = InStr(1, ContentEml.ToLower, StartFeld2.ToLower)
            iLenNoiDung = iVT2 - iVT1

            sRet = Mid(ContentEml, iVT1, iLenNoiDung)
        Catch ex As Exception

        End Try

        Return sRet
    End Function
End Class
